package com.teach.demo.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.RandomUtil;
import com.teach.CodeException;
import com.teach.annotation.CheckLogin;
import com.teach.annotation.CheckRole;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teach.demo.entity.Hello;

import lombok.extern.slf4j.Slf4j;

@RequestMapping("/demo/hello")
@RestController
@Slf4j
public class HelloController {

  @PostMapping
  public void doAdd(@RequestBody Hello hello){
    log.info("doAdd:{}",hello);
  }

  @PutMapping
  public void doUpdate(@RequestBody Hello hello){
    log.info("doUpdate:{}",hello);
  }

  @DeleteMapping("{id}")
  @CheckRole("admin")
  public void doDelete(@PathVariable String id){
    log.info("doDelete:{}",id);
  }

  @GetMapping("{id}")
  @CheckLogin
  public void doGet(@PathVariable String id){
    log.info("doGet:{}",id);
  }

  @GetMapping("error")
  public void doError(){
    if(RandomUtil.randomBoolean()){
      throw new CodeException(1, "测试错误");
    }
  }

  @GetMapping("list")
  public List<?> doList(){
    log.info("doList");
    return Arrays.asList(new Hello(1,"张三",new Date()),new Hello(2,"李四",new Date(0)));
  }

  @GetMapping("page")
  public Page<? super Object> doPage(Page<? super Object> page){
    log.info("doPage");
    page.setRecords(Arrays.asList(new Hello(1,"张三",new Date()),new Hello(2,"李四",new Date(0))));
    return page;
  }
  
}
